<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分析结果编号" prop="onlineResultNo">
        <el-input
          v-model="queryParams.onlineResultNo"
          placeholder="请输入分析结果编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="成分id" prop="constituentId">
        <el-input
          v-model="queryParams.constituentId"
          placeholder="请输入成分id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="成分名称" prop="constituentName">
        <el-input
          v-model="queryParams.constituentName"
          placeholder="请输入成分名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预测值" prop="predicted">
        <el-input
          v-model="queryParams.predicted"
          placeholder="请输入预测值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门名称" prop="departmentName">
        <el-input
          v-model="queryParams.departmentName"
          placeholder="请输入部门名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门id" prop="departmentId">
        <el-input
          v-model="queryParams.departmentId"
          placeholder="请输入部门id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仪器id" prop="instrumentId">
        <el-input
          v-model="queryParams.instrumentId"
          placeholder="请输入仪器id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仪器编号" prop="instrumentNo">
        <el-input
          v-model="queryParams.instrumentNo"
          placeholder="请输入仪器编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仪器名称" prop="instrumentName">
        <el-input
          v-model="queryParams.instrumentName"
          placeholder="请输入仪器名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模型id" prop="modelId">
        <el-input
          v-model="queryParams.modelId"
          placeholder="请输入模型id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模型编号" prop="modelNo">
        <el-input
          v-model="queryParams.modelNo"
          placeholder="请输入模型编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采样时间" prop="samplingTime">
        <el-date-picker clearable
          v-model="queryParams.samplingTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择采样时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否删除" prop="isDelete">
        <el-input
          v-model="queryParams.isDelete"
          placeholder="请输入是否删除"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="新增人id" prop="creatorId">
        <el-input
          v-model="queryParams.creatorId"
          placeholder="请输入新增人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="新增人" prop="creator">
        <el-input
          v-model="queryParams.creator"
          placeholder="请输入新增人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改人id" prop="modifierId">
        <el-input
          v-model="queryParams.modifierId"
          placeholder="请输入修改人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改人" prop="modifier">
        <el-input
          v-model="queryParams.modifier"
          placeholder="请输入修改人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改时间" prop="modifyTime">
        <el-date-picker clearable
          v-model="queryParams.modifyTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择修改时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="样品光谱id" prop="sampleSpectrumId">
        <el-input
          v-model="queryParams.sampleSpectrumId"
          placeholder="请输入样品光谱id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="样品光谱编号" prop="sampleSpectrumNo">
        <el-input
          v-model="queryParams.sampleSpectrumNo"
          placeholder="请输入样品光谱编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['analysis:zlb:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['analysis:zlb:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['analysis:zlb:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['analysis:zlb:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="zlbList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="分析结果id" align="center" prop="onlineResultId" />
      <el-table-column label="分析结果编号" align="center" prop="onlineResultNo" />
      <el-table-column label="成分id" align="center" prop="constituentId" />
      <el-table-column label="成分名称" align="center" prop="constituentName" />
      <el-table-column label="预测值" align="center" prop="predicted" />
      <el-table-column label="部门名称" align="center" prop="departmentName" />
      <el-table-column label="部门id" align="center" prop="departmentId" />
      <el-table-column label="仪器id" align="center" prop="instrumentId" />
      <el-table-column label="仪器编号" align="center" prop="instrumentNo" />
      <el-table-column label="仪器名称" align="center" prop="instrumentName" />
      <el-table-column label="模型id" align="center" prop="modelId" />
      <el-table-column label="模型编号" align="center" prop="modelNo" />
      <el-table-column label="采样时间" align="center" prop="samplingTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.samplingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="是否删除" align="center" prop="isDelete" />
      <el-table-column label="新增人id" align="center" prop="creatorId" />
      <el-table-column label="新增人" align="center" prop="creator" />
      <el-table-column label="修改人id" align="center" prop="modifierId" />
      <el-table-column label="修改人" align="center" prop="modifier" />
      <el-table-column label="修改时间" align="center" prop="modifyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.modifyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="样品光谱id" align="center" prop="sampleSpectrumId" />
      <el-table-column label="样品光谱编号" align="center" prop="sampleSpectrumNo" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['analysis:zlb:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['analysis:zlb:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改制粒二车间分析历史对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分析结果编号" prop="onlineResultNo">
          <el-input v-model="form.onlineResultNo" placeholder="请输入分析结果编号" />
        </el-form-item>
        <el-form-item label="成分id" prop="constituentId">
          <el-input v-model="form.constituentId" placeholder="请输入成分id" />
        </el-form-item>
        <el-form-item label="成分名称" prop="constituentName">
          <el-input v-model="form.constituentName" placeholder="请输入成分名称" />
        </el-form-item>
        <el-form-item label="预测值" prop="predicted">
          <el-input v-model="form.predicted" placeholder="请输入预测值" />
        </el-form-item>
        <el-form-item label="部门名称" prop="departmentName">
          <el-input v-model="form.departmentName" placeholder="请输入部门名称" />
        </el-form-item>
        <el-form-item label="部门id" prop="departmentId">
          <el-input v-model="form.departmentId" placeholder="请输入部门id" />
        </el-form-item>
        <el-form-item label="仪器id" prop="instrumentId">
          <el-input v-model="form.instrumentId" placeholder="请输入仪器id" />
        </el-form-item>
        <el-form-item label="仪器编号" prop="instrumentNo">
          <el-input v-model="form.instrumentNo" placeholder="请输入仪器编号" />
        </el-form-item>
        <el-form-item label="仪器名称" prop="instrumentName">
          <el-input v-model="form.instrumentName" placeholder="请输入仪器名称" />
        </el-form-item>
        <el-form-item label="模型id" prop="modelId">
          <el-input v-model="form.modelId" placeholder="请输入模型id" />
        </el-form-item>
        <el-form-item label="模型编号" prop="modelNo">
          <el-input v-model="form.modelNo" placeholder="请输入模型编号" />
        </el-form-item>
        <el-form-item label="采样时间" prop="samplingTime">
          <el-date-picker clearable
            v-model="form.samplingTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择采样时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否删除" prop="isDelete">
          <el-input v-model="form.isDelete" placeholder="请输入是否删除" />
        </el-form-item>
        <el-form-item label="新增人id" prop="creatorId">
          <el-input v-model="form.creatorId" placeholder="请输入新增人id" />
        </el-form-item>
        <el-form-item label="新增人" prop="creator">
          <el-input v-model="form.creator" placeholder="请输入新增人" />
        </el-form-item>
        <el-form-item label="修改人id" prop="modifierId">
          <el-input v-model="form.modifierId" placeholder="请输入修改人id" />
        </el-form-item>
        <el-form-item label="修改人" prop="modifier">
          <el-input v-model="form.modifier" placeholder="请输入修改人" />
        </el-form-item>
        <el-form-item label="修改时间" prop="modifyTime">
          <el-date-picker clearable
            v-model="form.modifyTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择修改时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="样品光谱id" prop="sampleSpectrumId">
          <el-input v-model="form.sampleSpectrumId" placeholder="请输入样品光谱id" />
        </el-form-item>
        <el-form-item label="样品光谱编号" prop="sampleSpectrumNo">
          <el-input v-model="form.sampleSpectrumNo" placeholder="请输入样品光谱编号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listZlb, getZlb, delZlb, addZlb, updateZlb } from "@/api/analysis/zlb";

export default {
  name: "Zlb",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 制粒二车间分析历史表格数据
      zlbList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        onlineResultNo: null,
        constituentId: null,
        constituentName: null,
        predicted: null,
        departmentName: null,
        departmentId: null,
        instrumentId: null,
        instrumentNo: null,
        instrumentName: null,
        modelId: null,
        modelNo: null,
        samplingTime: null,
        remarks: null,
        isDelete: null,
        creatorId: null,
        creator: null,
        modifierId: null,
        modifier: null,
        modifyTime: null,
        sampleSpectrumId: null,
        sampleSpectrumNo: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        onlineResultNo: [
          { required: true, message: "分析结果编号不能为空", trigger: "blur" }
        ],
        constituentId: [
          { required: true, message: "成分id不能为空", trigger: "blur" }
        ],
        constituentName: [
          { required: true, message: "成分名称不能为空", trigger: "blur" }
        ],
        predicted: [
          { required: true, message: "预测值不能为空", trigger: "blur" }
        ],
        departmentName: [
          { required: true, message: "部门名称不能为空", trigger: "blur" }
        ],
        departmentId: [
          { required: true, message: "部门id不能为空", trigger: "blur" }
        ],
        instrumentId: [
          { required: true, message: "仪器id不能为空", trigger: "blur" }
        ],
        instrumentNo: [
          { required: true, message: "仪器编号不能为空", trigger: "blur" }
        ],
        instrumentName: [
          { required: true, message: "仪器名称不能为空", trigger: "blur" }
        ],
        modelId: [
          { required: true, message: "模型id不能为空", trigger: "blur" }
        ],
        modelNo: [
          { required: true, message: "模型编号不能为空", trigger: "blur" }
        ],
        samplingTime: [
          { required: true, message: "采样时间不能为空", trigger: "blur" }
        ],
        isDelete: [
          { required: true, message: "是否删除不能为空", trigger: "blur" }
        ],
        creatorId: [
          { required: true, message: "新增人id不能为空", trigger: "blur" }
        ],
        creator: [
          { required: true, message: "新增人不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "新增时间不能为空", trigger: "blur" }
        ],
        sampleSpectrumId: [
          { required: true, message: "样品光谱id不能为空", trigger: "blur" }
        ],
        sampleSpectrumNo: [
          { required: true, message: "样品光谱编号不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询制粒二车间分析历史列表 */
    getList() {
      this.loading = true;
      listZlb(this.queryParams).then(response => {
        this.zlbList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        onlineResultId: null,
        onlineResultNo: null,
        constituentId: null,
        constituentName: null,
        predicted: null,
        departmentName: null,
        departmentId: null,
        instrumentId: null,
        instrumentNo: null,
        instrumentName: null,
        modelId: null,
        modelNo: null,
        samplingTime: null,
        remarks: null,
        isDelete: null,
        creatorId: null,
        creator: null,
        createTime: null,
        modifierId: null,
        modifier: null,
        modifyTime: null,
        sampleSpectrumId: null,
        sampleSpectrumNo: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.onlineResultId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加制粒二车间分析历史";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const onlineResultId = row.onlineResultId || this.ids
      getZlb(onlineResultId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改制粒二车间分析历史";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.onlineResultId != null) {
            updateZlb(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addZlb(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const onlineResultIds = row.onlineResultId || this.ids;
      this.$modal.confirm('是否确认删除制粒二车间分析历史编号为"' + onlineResultIds + '"的数据项？').then(function() {
        return delZlb(onlineResultIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('analysis/zlb/export', {
        ...this.queryParams
      }, `zlb_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
